import { useCurd, useForm, useTable } from '@/hooks/useCurd'
import { PaginationDto } from '@/modules/common/common.dto'
import { Injectable } from 'butterfly-decorators'
import { Application, ApplicationController } from './application.controller'

export const tableState = useTable<Application>()
export const formState = useForm<Application>()

export const init = () => {
  tableState.columns = [
    { type: 'selection' },
    { title: '应用Logo', key: 'applicationLogo', slot: true },
    { title: '应用名称', key: 'applicationName' },
    { title: '应用简介', key: 'applicationDesc' },
    { title: 'API_BASE_URL', key: 'apiBaseUrl' },
    { title: '数据库名称', key: 'databaseName' }
  ]
  formState.template = [
    {
      template: [
        {
          type: 'text',
          label: '应用名称',
          keyPath: 'applicationName',
          textProps: { placeholder: '请输入应用名称' }
        },
        {
          type: 'text',
          label: '应用文件夹名称',
          keyPath: 'applicationDir',
          textProps: { placeholder: '请输入应用文件夹名称' }
        },
        {
          type: 'textarea',
          label: '应用简介',
          keyPath: 'applicationDesc',
          textareaProps: { placeholder: '请输入应用简介' }
        },
        {
          type: 'upload',
          label: '应用logo',
          keyPath: 'applicationLogo',
          uploadProps: {
            action: import.meta.env.BUTTERFLY_BASE_API_URL + '/common/upload?type=APPLICATION_LOGO',
            listType: 'image-card',
            max: 1,
            name: 'files'
          }
        }
      ]
    },
    {
      tabTitle: '应用配置',
      tabKey: '2',
      template: [
        {
          type: 'text',
          label: 'API_BASE_URL',
          keyPath: 'apiBaseUrl',
          labelWidth: 110,
          textProps: { placeholder: '请输入接口基础请求地址' }
        },
        {
          type: 'text',
          label: 'mysql连接地址',
          keyPath: 'databaseUrl',
          labelWidth: 110,
          textProps: { placeholder: '请输入mysql连接地址和端口号' }
        },
        {
          type: 'text',
          label: '数据库用户名',
          keyPath: 'databaseUserName',
          labelWidth: 110,
          textProps: { placeholder: '请输入mysql用户名' }
        },
        {
          type: 'text',
          label: '数据库密码',
          keyPath: 'databasePassword',
          labelWidth: 110,
          textProps: { placeholder: '请输入mysql密码' }
        },
        {
          type: 'text',
          label: '数据库名称',
          keyPath: 'databaseName',
          labelWidth: 110,
          textProps: { placeholder: '请输入数据库名称' }
        }
      ]
    }
  ]
}

@Injectable({
  controllers: [ApplicationController]
})
export class ApplicationService {
  static applicationController: ApplicationController

  static async getApplicationList({ page, size }: PaginationDto) {
    const res = await this.applicationController.findListReq({ page, size })!
    if (res.code === 200) {
      res.data.list.forEach((item) => {
        item.applicationLogo = item.applicationLogo
          ? import.meta.env.BUTTERFLY_BASE_API_URL + item.applicationLogo
          : ''
      })
      return { list: res.data.list, total: res.data.pageInfo.total }
    }
    return { list: [], total: 0 }
  }

  static async createOne(newData: Application) {
    const res = await this.applicationController.addOneReq(newData)!
    if (res.code == 200) {
      return { success: true, msg: '操作成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }

  static async updateOne(newData: Application) {
    const res = await this.applicationController.updateOneReq(newData)!
    if (res.code === 200) {
      return { success: true, msg: '操作成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }

  static async deleteByIds(ids: any[]) {
    const res = await this.applicationController.deleteByIdsReq(ids)!
    if (res.code === 200) {
      return { success: true, msg: '删除成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }
}

export const { r, c, u, d } = useCurd({
  r(page, size) {
    return ApplicationService.getApplicationList({ page, size })
  },
  c(newData) {
    return ApplicationService.createOne(newData)
  },
  u(newData) {
    return ApplicationService.updateOne(newData)
  },
  d(keys) {
    return ApplicationService.deleteByIds(keys)
  }
})
